package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.res.model.entity.ResProBomRecord;
import com.wing.res.model.entity.ResProTask;
import com.wing.res.service.ResProTaskService;
import com.wing.sell.dao.OrderProEnclosureDao;
import com.wing.sell.model.entity.OrderProEnclosure;
import com.wing.sell.model.request.OrderProEnclosureForm;
import com.wing.sell.model.response.OrderProEnclosureVO;
import com.wing.sell.service.OrderProEnclosureService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 销售订单产品附件表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderProEnclosureServiceImpl extends ServiceImpl<OrderProEnclosureDao, OrderProEnclosure> implements OrderProEnclosureService {

    @Resource
    private ResProTaskService resProTaskService;


    @Override
    public int add(OrderProEnclosureForm orderProEnclosureForm) {

        //校验

        OrderProEnclosure orderProEnclosure =new OrderProEnclosure();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderProEnclosure.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderProEnclosureForm, orderProEnclosure, copyOptions);

        return baseMapper.insert(orderProEnclosure);
    }

    @Override
    public List<OrderProEnclosureVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderProEnclosureVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderProEnclosureVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderProEnclosureVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    /**
     * 点击确认
     * @param id
     * @return
     */
    @Override
    public int confirm(String id) {
        //1. 修改状态
        ResProTask byId = resProTaskService.getById(id);
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        byId.setUpdateTime(LocalDateTime.now());
        byId.setIsCraftsmanship(ProductEnum.dictEmum.S.getCode());
        if (byId.getIsBom().equals(ProductEnum.dictEmum.S.getCode())) {
            byId.setStatus(MaintenanceEnum.Task_Status_Enum.DJD.getCode());
        }
        boolean updateById = this.resProTaskService.updateById(byId);
        if (updateById) {
            return 1;
        }
        return 0;
    }
}
